﻿DELIMITER //

--
--
DROP PROCEDURE IF EXISTS dsZeusPickingList
//

-- HU - Kiszedési lista
-- EN - Picking list
--
CREATE PROCEDURE dsZeusPickingList 
  (_AccessToken INTEGER)
BEGIN
  DECLARE _ID INTEGER;

  DECLARE _Rows INTEGER;
  DECLARE _UserID INTEGER;
  DECLARE _Lang VARCHAR(10);

  --
  --
  CALL spAccessTokenCheck(_AccessToken, _UserID, _Lang);


  BEGIN
    DECLARE EXIT HANDLER FOR NOT FOUND SET _Rows = 0;

    SELECT ID
      INTO _ID
      FROM ZeusPicking 
     WHERE (Status = 'O') AND ((UserID IS NULL) OR (UserID = _UserID)) 
     LIMIT 1;

    SET _Rows = 1;
  END;


  -- Update
  --
  BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION SET _Rows = 0;

    -- HU - Lefoglal egy új kiszedési listát vagy jó lesz egy már megkezdett
    -- EN - Get a new picking list for this user OR already existing
    --
    UPDATE ZeusPicking 
       SET UserID = _UserID
     WHERE ID = _ID;

    SET _Rows = ROW_COUNT();
  END;


  -- HU - Kiszedési lista adatai
  -- EN - Get the picking details
  --
  SELECT p.ID, p.Code, p.PartnerName,
         
         i.ID ItemID, i.Code ItemCode, i.LocationCode, i.ArticleCode, i.ArticleName, 

         CAST(i.Expired as DATE) Expired, i.Quantity, i.UnitCode, i.PickedQua, '' Notes

    FROM ZeusPicking p
    JOIN ZeusPickingItem i ON p.ID = i.PickingID
   WHERE p.ID = _ID
   ORDER BY i.LocationCode;

END
//
